package cn.com.twoke.application.game.mario.core.ecs.util;

import cn.com.twoke.application.game.mario.core.ecs.component.Vector2f;

import java.awt.*;

/**
 * 对象池管理器，管理游戏中的各种对象池
 */
public class PoolManager {
    // Vector2f对象池
    private static final ObjectPool<Vector2f> vector2fPool = new ObjectPool<>(
        () -> new Vector2f(0, 0), 
        50,  // 初始大小
        200  // 最大大小
    );
    
    // Rectangle对象池
    private static final ObjectPool<Rectangle> rectanglePool = new ObjectPool<>(
        () -> new Rectangle(0, 0, 0, 0),
        50,  // 初始大小
        200  // 最大大小
    );

    /**
     * 获取Vector2f对象
     *
     * @param x x坐标
     * @param y y坐标
     * @return Vector2f实例
     */
    public static Vector2f acquireVector2f(float x, float y) {
        Vector2f vector = vector2fPool.acquire();
        if (vector != null) {
            vector.x = x;
            vector.y = y;
        } else {
            vector = new Vector2f(x, y);
        }
        return vector;
    }

    /**
     * 归还Vector2f对象
     *
     * @param vector Vector2f实例
     */
    public static void releaseVector2f(Vector2f vector) {
        if (vector != null) {
            vector2fPool.release(vector);
        }
    }

    /**
     * 获取Rectangle对象
     *
     * @param x x坐标
     * @param y y坐标
     * @param width 宽度
     * @param height 高度
     * @return Rectangle实例
     */
    public static Rectangle acquireRectangle(int x, int y, int width, int height) {
        Rectangle rect = rectanglePool.acquire();
        if (rect != null) {
            rect.setBounds(x, y, width, height);
        } else {
            rect = new Rectangle(x, y, width, height);
        }
        return rect;
    }

    /**
     * 归还Rectangle对象
     *
     * @param rect Rectangle实例
     */
    public static void releaseRectangle(Rectangle rect) {
        if (rect != null) {
            rectanglePool.release(rect);
        }
    }

    /**
     * 获取对象池统计信息
     *
     * @return 统计信息
     */
    public static String getPoolStats() {
        return String.format("Vector2f Pool: %s | Rectangle Pool: %s",
            vector2fPool.getStats(), rectanglePool.getStats());
    }
    
    /**
     * 重置接口
     */
    public interface Resetable {
        /**
         * 重置对象状态
         */
        void reset();
    }
}